home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / lib / readn.c < prev    next >
Text File  |  1989-12-17  |  467b  |  27 lines

  1. /*
  2.  * Read "n" bytes from a descriptor.
  3.  * Use in place of read() when fd is a stream socket.
  4.  */
  5.  
  6. int
  7. readn(fd, ptr, nbytes)
  8. register int    fd;
  9. register char    *ptr;
  10. register int    nbytes;
  11. {
  12.     int    nleft, nread;
  13.  
  14.     nleft = nbytes;
  15.     while (nleft > 0) {
  16.         nread = read(fd, ptr, nleft);
  17.         if (nread < 0)
  18.             return(nread);        /* error, return < 0 */
  19.         else if (nread == 0)
  20.             break;            /* EOF */
  21.  
  22.         nleft -= nread;
  23.         ptr   += nread;
  24.     }
  25.     return(nbytes - nleft);        /* return >= 0 */
  26. }
  27.